package com.xm.design_patterns;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Author：xm
 * Project：interview
 * Date：2025/8/4  14:52
 */

// 测试懒汉模式的线程安全 _02_Singleton_Lazy
public class _02_Singleton_LazyTest {
    public static void main(String[] args) {
        Set<String> instanceSet = Collections.synchronizedSet(new HashSet<>());
        // 若是没出现，试这多运行几次或调大循环次数
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
            new Thread(() -> instanceSet.add(_02_Singleton_Lazy.getInstance().toString())).start();
        }
        // try {
        //     Thread.sleep(5000);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }
        System.out.println(instanceSet.size());
        System.out.println(instanceSet);
    }
}
